package com.mscsoft.ui;

import com.mscsoft.App;
import com.mscsoft.musicscore.MusicScore;
import com.mscsoft.tools.ConTools;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.stage.Stage;

public class GameEndScene extends Application {

    @FXML
    private Label apLabel;

    @FXML
    private ImageView background;

    @FXML
    private Label fcLabel;

    @FXML
    private HBox fcapBox;

    @FXML
    private VBox goodBox;

    @FXML
    private HBox infoDetailBox;

    @FXML
    private VBox infoMessage;

    @FXML
    private Label lastScoreLabel;

    @FXML
    private Label numOfGood;

    @FXML
    private Label numOfMaxCombo;

    @FXML
    private Label numOfMiss;

    @FXML
    private Label numOfPerfect;

    @FXML
    private Label numOfPerfectPlus;

    @FXML
    private VBox othersBox;

    @FXML
    private VBox perfectBox;

    @FXML
    private VBox perfectPlusBox;

    @FXML
    private ImageView rankImage;

    @FXML
    private Label rankLabel;

    @FXML
    private Button retryButton;

    @FXML
    private Button returnButton;

    @FXML
    private Pane root;

    @FXML
    private ImageView songImage;

    @FXML
    private Label textOfGood;

    @FXML
    private Label textOfMaxCombo;

    @FXML
    private Label textOfMiss;

    @FXML
    private Label textOfPerfect;

    @FXML
    private Label textOfPerfectPlus;

    @FXML
    private HBox userInfo;

    @FXML
    void retry(ActionEvent event) throws Exception {
        Play play = new Play();
        play.setMS(retryMs);
        play.start(new Stage());
        Platform.runLater(stage::close);
    }

    private static String rating = null;
    private static int score = -1;
    private static int maxCombo = -1;
    private static int[] counts = { 0, 0, 0, 0, 0 };
    private static int bestScore = -1;
    public static final Stage stage = new Stage();

    private static boolean offline = App.getOffline();
    private static MusicScore retryMs;

    public static void set(String r, int s, int m, int[] c, MusicScore ms) {
        rating = r;
        score = s;
        maxCombo = m;
        counts = c;
        retryMs = ms;
        bestScore = offline ? -1 : ConTools.getBestScore(ms.meta.id).getScore();
    }

    @FXML
    void returnSelectScene(ActionEvent event) {
        Platform.runLater(() -> {
            try {
                SelectSongsScene selectSongsScene = new SelectSongsScene();
                selectSongsScene.start(SelectSongsScene.getStage());
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        Platform.runLater(stage::close);
    }

    @FXML
    public void initialize() {
        // retry按钮样式
        Image retryImage = new Image(Play.class.getResourceAsStream("/static/img/GameEndImages/retry.png"));
        BackgroundImage retryBgImage = new BackgroundImage(retryImage, BackgroundRepeat.NO_REPEAT,
                BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER,
                new BackgroundSize(retryButton.getWidth(), retryButton.getHeight(), true, true, true, false));
        Background retryBackground = new Background(retryBgImage);
        retryButton.setBackground(retryBackground);
        // 返回按钮样式
        Image returnImage = new Image(Play.class.getResourceAsStream("/static/img/GameEndImages/back.png"));
        BackgroundImage returnBgImage = new BackgroundImage(returnImage, BackgroundRepeat.NO_REPEAT,
                BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER,
                new BackgroundSize(returnButton.getWidth(), returnButton.getHeight(), true, true, true, false));
        Background returnBackground = new Background(returnBgImage);
        returnButton.setBackground(returnBackground);
        // 曲绘及背景图片添加
        setBackgroundImage();
        setSongImage();
        // Rank图标添加
        rankImage.setImage(getRankImage());
        // 分数显示
        rankLabel.setText(getScore());
        // 上次最佳分数显示
        lastScoreLabel.setText(getLastBestScore());
        // P+个数显示
        numOfPerfectPlus.setText(getPerfectNum());
        // P个数显示
        numOfPerfect.setText(getGoodNum());
        // G个数显示
        numOfGood.setText(getBadNum());
        // MaxCombo个数显示
        numOfMaxCombo.setText(getMaxComboNum());
        // M个数显示
        numOfMiss.setText(getMissNum());
        // 返回按钮事件监听
        returnButton.setOnAction(this::returnSelectScene);
    }

    public Image getBackgroundImage() {
        return new Image(Play.class.getResourceAsStream("/static/img/GameEndImages/backgroundImage.png"));
    }

    public Image getSongImage() {
        return new Image(Play.class.getResourceAsStream("/static/img/GameEndImages/songImage.png"));
    }

    public Image getRankImage() {
        return new Image(Play.class.getResourceAsStream("/static/img/rating/" + rating + ".png"));
    }

    public String getScore() {
        return "Your Score : " + score;
    }

    public String getLastBestScore() {
        return "Your Last Best Score : " + bestScore;
    }

    public String getPerfectNum() {
        return counts[0] + "+" + counts[1];
    }

    public String getGoodNum() {
        return "" + counts[2];
    }

    public String getBadNum() {
        return "" + counts[3];
    }

    public String getMissNum() {
        return "" + counts[4];
    }

    public String getMaxComboNum() {
        return "" + maxCombo;
    }

    @FXML
    public void setBackgroundImage() {
        background.setImage(getBackgroundImage());
    }

    @FXML
    public void setSongImage() {
        songImage.setImage(getSongImage());
    }

    @Override
    @FXML
    public void start(Stage primaryStage) throws Exception {
        Parent proot = new FXMLLoader().load(getClass().getResourceAsStream("/fxml/GameEndScene.fxml"));
        primaryStage.setTitle("GameEndScene");
        primaryStage.setScene(new Scene(proot, App.getResolutionX(), App.getResolutionY()));
        primaryStage.setOnCloseRequest(e -> System.exit(0));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}
